#Data subset with smaller bandwidths
robust_df_con6 <- bes_w22_covid |> 
  filter(partyId==1,date_diff<=5,date_diff>=-6)
robust_df_con5 <- bes_w22_covid |> 
  filter(partyId==1,date_diff<=4,date_diff>=-5)
robust_df_con4 <- bes_w22_covid |> 
  filter(partyId==1,date_diff<=3,date_diff>=-4)
robust_df_con3 <- bes_w22_covid |> 
  filter(partyId==1,date_diff<=2,date_diff>=-3)

robust_df_lab6 <- bes_w22_covid |> 
  filter(partyId==2,date_diff<=5,date_diff>=-6)
robust_df_lab5 <- bes_w22_covid |> 
  filter(partyId==2,date_diff<=4,date_diff>=-5)
robust_df_lab4 <- bes_w22_covid |> 
  filter(partyId==2,date_diff<=3,date_diff>=-4)
robust_df_lab3 <- bes_w22_covid |> 
  filter(partyId==2,date_diff<=2,date_diff>=-3)

robust_df_ind6 <- bes_w22_covid |> 
  filter(partyId==10,date_diff<=5,date_diff>=-6)
robust_df_ind5 <- bes_w22_covid |> 
  filter(partyId==10,date_diff<=4,date_diff>=-5)
robust_df_ind4 <- bes_w22_covid |> 
  filter(partyId==10,date_diff<=3,date_diff>=-4)
robust_df_ind3 <- bes_w22_covid |> 
  filter(partyId==10,date_diff<=2,date_diff>=-3)

robust_con_list <- list(robust_df_con6,robust_df_con5,
                        robust_df_con4,robust_df_con3)
robust_lab_list <- list(robust_df_lab6,robust_df_lab5,
                        robust_df_lab4,robust_df_lab3)
robust_ind_list <- list(robust_df_ind6,robust_df_ind5,
                        robust_df_ind4,robust_df_ind3)

con_robust_est <- tibble(est=NA,std=NA,
                         party=rep(c("Cons"),24),
                         dv=c(rep("Like Cons (0-10)",4),rep("Like Johnson (0-10)",4),
                              rep("COVID-19 Performance (1-5)",4),
                              rep("Lockdown Performance (1-5)",4),
                              rep("Vote Intention: Con (0-1)",4),
                              rep("Vote Intention: Lab (0-1)",4)),
                         bw=c(rep(c(6,5,4,3),6)))

lab_robust_est <- tibble(est=NA,std=NA,
                         party=rep(c("Lab"),24),
                         dv=c(rep("Like Cons (0-10)",4),rep("Like Johnson (0-10)",4),
                              rep("COVID-19 Performance (1-5)",4),
                              rep("Lockdown Performance (1-5)",4),
                              rep("Vote Intention: Con (0-1)",4),
                              rep("Vote Intention: Lab (0-1)",4)),
                         bw=c(rep(c(6,5,4,3),6)))

ind_robust_est <- tibble(est=NA,std=NA,
                         party=rep(c("Ind"),24),
                         dv=c(rep("Like Cons (0-10)",4),rep("Like Johnson (0-10)",4),
                              rep("COVID-19 Performance (1-5)",4),
                              rep("Lockdown Performance (1-5)",4),
                              rep("Vote Intention: Con (0-1)",4),
                              rep("Vote Intention: Lab (0-1)",4)),
                         bw=c(rep(c(6,5,4,3),6)))
for (i in 1:4) {
  likecon_con_temp <- lm(likeCon~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_con_list[[i]] |> 
                           filter(likeCon<=10))
  con_robust_est$est[i] <- 
    coefficients(summary(likecon_con_temp))[2,1]
  con_robust_est$std[i] <- 
    coefficients(summary(likecon_con_temp))[2,2]
  
  likejohnson_con_temp <- lm(likeJohnson~treatment + age + ctr_gender + ctr_education +
                               ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                               daily_new_confirmed_per_million,
                             data = robust_con_list[[i]] |> 
                               filter(likeJohnson<=10))
  con_robust_est$est[i+4] <- 
    coefficients(summary(likejohnson_con_temp))[2,1]
  con_robust_est$std[i+4] <- 
    coefficients(summary(likejohnson_con_temp))[2,2]
  
  covid_con_temp <- lm(handleCorona~treatment + age + ctr_gender + ctr_education +
                         ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                         daily_new_confirmed_per_million,
                       data = robust_con_list[[i]] |> 
                         filter(handleCorona<=10))
  con_robust_est$est[i+8] <- 
    coefficients(summary(covid_con_temp))[2,1]
  con_robust_est$std[i+8] <- 
    coefficients(summary(covid_con_temp))[2,2]
  
  lockdown_con_temp <- lm(govtHandlelockdown~treatment + age + ctr_gender + ctr_education +
                            ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                            daily_new_confirmed_per_million,
                          data = robust_con_list[[i]] |> 
                            filter(govtHandlelockdown<=10))
  con_robust_est$est[i+12] <- 
    coefficients(summary(lockdown_con_temp))[2,1]
  con_robust_est$std[i+12] <- 
    coefficients(summary(lockdown_con_temp))[2,2]
  
  convote_con_temp <- lm(convote~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_con_list[[i]])
  con_robust_est$est[i+16] <- 
    coefficients(summary(convote_con_temp))[2,1]
  con_robust_est$std[i+16] <- 
    coefficients(summary(convote_con_temp))[2,2]
  
  labvote_con_temp <- lm(labvote~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_con_list[[i]])
  con_robust_est$est[i+20] <- 
    coefficients(summary(labvote_con_temp))[2,1]
  con_robust_est$std[i+20] <- 
    coefficients(summary(labvote_con_temp))[2,2]
}


for (i in 1:4) {
  likecon_lab_temp <- lm(likeCon~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_lab_list[[i]] |> 
                           filter(likeCon<=10))
  lab_robust_est$est[i] <- 
    coefficients(summary(likecon_lab_temp))[2,1]
  lab_robust_est$std[i] <- 
    coefficients(summary(likecon_lab_temp))[2,2]
  
  likejohnson_lab_temp <- lm(likeJohnson~treatment + age + ctr_gender + ctr_education +
                               ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                               daily_new_confirmed_per_million,
                             data = robust_lab_list[[i]] |> 
                               filter(likeJohnson<=10))
  lab_robust_est$est[i+4] <- 
    coefficients(summary(likejohnson_lab_temp))[2,1]
  lab_robust_est$std[i+4] <- 
    coefficients(summary(likejohnson_lab_temp))[2,2]
  
  covid_lab_temp <- lm(handleCorona~treatment + age + ctr_gender + ctr_education +
                         ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                         daily_new_confirmed_per_million,
                       data = robust_lab_list[[i]] |> 
                         filter(handleCorona<=10))
  lab_robust_est$est[i+8] <- 
    coefficients(summary(covid_lab_temp))[2,1]
  lab_robust_est$std[i+8] <- 
    coefficients(summary(covid_lab_temp))[2,2]
  
  lockdown_lab_temp <- lm(govtHandlelockdown~treatment + age + ctr_gender + ctr_education +
                            ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                            daily_new_confirmed_per_million,
                          data = robust_lab_list[[i]] |> 
                            filter(govtHandlelockdown<=10))
  lab_robust_est$est[i+12] <- 
    coefficients(summary(lockdown_lab_temp))[2,1]
  lab_robust_est$std[i+12] <- 
    coefficients(summary(lockdown_lab_temp))[2,2]
  
  convote_lab_temp <- lm(convote~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_lab_list[[i]])
  lab_robust_est$est[i+16] <- 
    coefficients(summary(convote_lab_temp))[2,1]
  lab_robust_est$std[i+16] <- 
    coefficients(summary(convote_lab_temp))[2,2]
  
  labvote_lab_temp <- lm(labvote~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_lab_list[[i]])
  lab_robust_est$est[i+20] <- 
    coefficients(summary(labvote_lab_temp))[2,1]
  lab_robust_est$std[i+20] <- 
    coefficients(summary(labvote_lab_temp))[2,2]
}



for (i in 1:4) {
  likecon_ind_temp <- lm(likeCon~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_ind_list[[i]] |> 
                           filter(likeCon<=10))
  ind_robust_est$est[i] <- 
    coefficients(summary(likecon_ind_temp))[2,1]
  ind_robust_est$std[i] <- 
    coefficients(summary(likecon_ind_temp))[2,2]
  
  likejohnson_ind_temp <- lm(likeJohnson~treatment + age + ctr_gender + ctr_education +
                               ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                               daily_new_confirmed_per_million,
                             data = robust_ind_list[[i]] |> 
                               filter(likeJohnson<=10))
  ind_robust_est$est[i+4] <- 
    coefficients(summary(likejohnson_ind_temp))[2,1]
  ind_robust_est$std[i+4] <- 
    coefficients(summary(likejohnson_ind_temp))[2,2]
  
  covid_ind_temp <- lm(handleCorona~treatment + age + ctr_gender + ctr_education +
                         ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                         daily_new_confirmed_per_million,
                       data = robust_ind_list[[i]] |> 
                         filter(handleCorona<=10))
  ind_robust_est$est[i+8] <- 
    coefficients(summary(covid_ind_temp))[2,1]
  ind_robust_est$std[i+8] <- 
    coefficients(summary(covid_ind_temp))[2,2]
  
  lockdown_ind_temp <- lm(govtHandlelockdown~treatment + age + ctr_gender + ctr_education +
                            ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                            daily_new_confirmed_per_million,
                          data = robust_ind_list[[i]] |> 
                            filter(govtHandlelockdown<=10))
  ind_robust_est$est[i+12] <- 
    coefficients(summary(lockdown_ind_temp))[2,1]
  ind_robust_est$std[i+12] <- 
    coefficients(summary(lockdown_ind_temp))[2,2]
  
  convote_ind_temp <- lm(convote~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_ind_list[[i]])
  ind_robust_est$est[i+16] <- 
    coefficients(summary(convote_ind_temp))[2,1]
  ind_robust_est$std[i+16] <- 
    coefficients(summary(convote_ind_temp))[2,2]
  
  labvote_ind_temp <- lm(labvote~treatment + age + ctr_gender + ctr_education +
                           ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
                           daily_new_confirmed_per_million,
                         data = robust_ind_list[[i]])
  ind_robust_est$est[i+20] <- 
    coefficients(summary(labvote_ind_temp))[2,1]
  ind_robust_est$std[i+20] <- 
    coefficients(summary(labvote_ind_temp))[2,2]
}

robust_est_combined <- 
  bind_rows(con_robust_est, lab_robust_est, ind_robust_est) |>
  mutate(
    Party = factor(party, levels = c("Cons", "Lab", "Ind")),
    ll_95 = est - 1.96 * std,
    ul_95 = est + 1.96 * std,
    ll_90 = est - 1.645 * std,
    ul_90 = est + 1.645 * std
  )


gg_likecon_smaller <- robust_est_combined |> 
  filter(dv == "Like Cons (0-10)") |> 
  ggplot(aes(x = bw, y = est)) +
  geom_pointrange(aes(ymin = ll_95, ymax = ul_95), linewidth = 1, color = "gray40") +
  geom_pointrange(aes(ymin = ll_90, ymax = ul_90), linewidth=2, color = "black",
                  size=1.5) +
  facet_wrap(~Party) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle("DV: Like Cons (0-10)") +
  labs(x = "Bandwidths", y = "Estimate") +
  theme_light() +
  theme(
    text = element_text(size = 30),
    plot.title = element_text(hjust = 0.5),
    panel.grid = element_blank(),
    strip.text=element_text(size=26,color="black"),
    strip.background = element_rect(fill="grey90")
  )

gg_likecon_smaller


gg_likejohnson_smaller <- robust_est_combined |> filter(dv=="Like Johnson (0-10)") |> 
  ggplot(aes(x = bw, y = est)) +
  geom_pointrange(aes(ymin = ll_95, ymax = ul_95), linewidth = 1, color = "gray40") +
  geom_pointrange(aes(ymin = ll_90, ymax = ul_90), linewidth=2, color = "black",
                  size=1.5) +
  facet_wrap(~Party)+
  geom_hline(yintercept = 0,linetype=2)+
  ggtitle("DV: Like Johnson (0-10)_")+
  labs(x="Bandwidths",y="Estimate")+
  theme_light()+
  theme(text = element_text(size=30),
        plot.title = element_text(hjust = 0.5),
        panel.grid = element_blank(),
        strip.text=element_text(size=26,color="black"),
        strip.background = element_rect(fill="grey90"))
gg_likejohnson_smaller

gg_covid_smaller <- robust_est_combined |> filter(dv=="COVID-19 Performance (1-5)") |> 
  ggplot(aes(x = bw, y = est)) +
  geom_pointrange(aes(ymin = ll_95, ymax = ul_95), linewidth = 1, color = "gray40") +
  geom_pointrange(aes(ymin = ll_90, ymax = ul_90), linewidth=2, color = "black",
                  size=1.5) +
  facet_wrap(~Party)+
  geom_hline(yintercept = 0,linetype=2)+
  ggtitle("DV: COVID-19 Performance (1-5)")+
  labs(x="Bandwidths",y="Estimate")+
  theme_light()+
  theme(text = element_text(size=30),
        plot.title = element_text(hjust = 0.5),
        panel.grid = element_blank(),
        strip.text=element_text(size=26,color="black"),
        strip.background = element_rect(fill="grey90"))
gg_covid_smaller

gg_lockdown_smaller <- robust_est_combined |> filter(dv=="Lockdown Performance (1-5)") |> 
  ggplot(aes(x = bw, y = est)) +
  geom_pointrange(aes(ymin = ll_95, ymax = ul_95), linewidth = 1, color = "gray40") +
  geom_pointrange(aes(ymin = ll_90, ymax = ul_90), linewidth=2, color = "black",
                  size=1.5) +
  facet_wrap(~Party)+
  geom_hline(yintercept = 0,linetype=2)+
  ggtitle("DV: Lockdown Performance (1-5)")+
  labs(x="Bandwidths",y="Estimate")+
  theme_light()+
  theme(text = element_text(size=30),
        plot.title = element_text(hjust = 0.5),
        panel.grid= element_blank(),
        strip.text=element_text(size=26,color="black"),
        strip.background = element_rect(fill="grey90"))
gg_lockdown_smaller

gg_convote_smaller <- robust_est_combined |> filter(dv=="Vote Intention: Con (0-1)") |> 
  ggplot(aes(x = bw, y = est)) +
  geom_pointrange(aes(ymin = ll_95, ymax = ul_95), linewidth = 1, color = "gray40") +
  geom_pointrange(aes(ymin = ll_90, ymax = ul_90), linewidth=2, color = "black",
                  size=1.5) +
  facet_wrap(~Party)+
  geom_hline(yintercept = 0,linetype=2)+
  ggtitle("DV: Vote Intention: Con (0-1)")+
  labs(x="Bandwidths",y="Estimate")+
  theme_light()+
  theme(text = element_text(size=30),
        plot.title = element_text(hjust = 0.5),
        panel.grid = element_blank(),
        strip.text=element_text(size=26,color="black"),
        strip.background = element_rect(fill="grey90"))
gg_convote_smaller

gg_labvote_smaller <- robust_est_combined |> filter(dv=="Vote Intention: Lab (0-1)") |> 
  ggplot(aes(x = bw, y = est)) +
  geom_pointrange(aes(ymin = ll_95, ymax = ul_95), linewidth = 1, color = "gray40") +
  geom_pointrange(aes(ymin = ll_90, ymax = ul_90), linewidth=2, color = "black",
                  size=1.5) +
  facet_wrap(~Party)+
  geom_hline(yintercept = 0,linetype=2)+
  ggtitle("DV: Vote Intention: Lab (0-1)")+
  labs(x="Bandwidths",y="Estimate")+
  theme_light()+
  theme(text = element_text(size=30),
        plot.title = element_text(hjust = 0.5),
        panel.grid  = element_blank(),
        strip.text=element_text(size=26,color="black"),
        strip.background = element_rect(fill="grey90"))
gg_labvote_smaller
